package com.jsbs.iam.user.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jsbs.iam.common.core.utils.PageHelperTool;
import com.jsbs.iam.user.dto.IamApplyDto;
import com.jsbs.iam.user.entity.IamApplySystem;
import com.jsbs.iam.user.entity.IamUserInfo;
import com.jsbs.iam.user.mapper.ApplyTypeMapper;
import com.jsbs.iam.user.mapper.IamApplySystemMapper;
import com.jsbs.iam.user.mapper.IamApplyTypeBaseMapper;
import com.jsbs.iam.user.mapper.UserInfoMapper;
import com.jsbs.iam.user.service.IApplySystemService;
import com.jsbs.iam.user.service.UserInfoService;
import com.jsbs.iam.user.vo.IamApplyVo;
import com.jsbs.iam.user.vo.IamTypeSystemVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author zhouxiangyang
 * @className ApplySystemServiceImpl
 * @date 2022/12/8 21:09
 * @description:
 */
@Slf4j
@Service
public class ApplySystemServiceImpl implements IApplySystemService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ApplySystemServiceImpl.class);

    @Autowired
    private IamApplySystemMapper iamApplySystemMapper;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private IamApplyTypeBaseMapper iamApplyTypeBaseMapper;

    @Autowired
    private ApplyTypeMapper applyTypeMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 功能描述：获取当前用户应用权限列表
     * @param userCode
     * @return
     * @throws Exception
     */
    @Override
    public List<IamApplySystem> getApplySystem(String userCode, String companyCode,String applyUrl){
        LOGGER.info("根据用户id查询用户下的应用列表开始,入参为：{}",userCode);

        // 根据当前用户获取启组织范围
        List<String> orgCodeList = userInfoService.queryOrgCode(userCode, companyCode);
        // 用户归属组织类型为空，则返回应用范围为空
        if (CollectionUtils.isEmpty(orgCodeList)) {
            return new ArrayList<>();
        }
        // 根据用户组织范围获取应用类型编码
        List<String> typeCodeList = applyTypeMapper.queryByOrgCode(orgCodeList);
        if (CollectionUtils.isEmpty(typeCodeList)) {
            return new ArrayList<>();
        }
        String url = iamApplyTypeBaseMapper.queryApplyUrl(typeCodeList.get(0));
        if (!url.equals(applyUrl)) {
            throw new RuntimeException("前端传入url错误，请检查后重试");
        }
        //根据应用类型编码查询应用
        List<IamApplySystem> applySystem = iamApplySystemMapper.queryByTypeCode(typeCodeList);
        if (CollectionUtils.isEmpty(applySystem)) {
            return new ArrayList<>();
        }
        return applySystem;
    }

    /**
     * 应用类型下拉框
     * @return
     */
    @Override
    public List<String> getApplySystemAll() {
        //查询所有应用类型名称 （应用类型下拉功能）
        List<String> applySystemAll = iamApplyTypeBaseMapper.getApplyTypeNameAll();
        return applySystemAll;
    }

    /**
     * 登录策略分页查询
     * @param iamApplyDto
     * @return
     */
    @Override
    public PageInfo<IamApplyVo> getIamApplyPage(IamApplyDto iamApplyDto,String companyCode) {
        //初始化分页
        iamApplyDto.init();
        PageHelper.startPage(iamApplyDto.getCurrPage(),iamApplyDto.getPageSize());
        //登录策略展示 （包括范围展示）
        List<IamApplyVo> appTypeCode = iamApplySystemMapper.getAppTypeCode(iamApplyDto.getApplyCodeOrName(),companyCode);
        PageInfo<IamApplyVo> iamApplyVoPageInfo = null;
        if (CollectionUtils.isNotEmpty(appTypeCode)) {
            PageInfo<IamApplyVo> pageInfo = new PageInfo<>(appTypeCode);
            int total = (int) pageInfo.getTotal();
            iamApplyVoPageInfo = PageHelperTool.initPageInfoObj(
                            iamApplyDto.getCurrPage(), total, iamApplyDto.getPageSize(), pageInfo);
        } else {
            LOGGER.info("登录策略未查到任何数据");
        }
        return iamApplyVoPageInfo;
    }

    @Override
    public List<IamApplySystem> queryList(String companyCode) {
        return iamApplySystemMapper.queryList(companyCode);
    }

    /**
     * 登录策略搜索(模糊搜索)
     * @param applyCodeOrName
     * @return
     */
    @Override
    public List<IamApplyVo> getApplyCodeOrName(String applyCodeOrName) {
        List<IamApplyVo> IamApplyVoList = iamApplySystemMapper.getApplyCodeOrName(applyCodeOrName);
        return IamApplyVoList;
    }

    /**
     *
     * @param applyName
     * @return
     */
    @Override
    public String getApplyTypeCode(String applyName) {
        String applyTypeCode = iamApplyTypeBaseMapper.getApplyTypeCode(applyName);
        return applyTypeCode;
    }

    @Override
    public String getApplyTypeName(String applyTypeCode) {
        String applyName = iamApplyTypeBaseMapper.getApplyTypeName(applyTypeCode);
        return applyName;
    }

    @Override
    public IamApplySystem queryByRequestUrl(String requestUrl){
        return iamApplySystemMapper.queryByRequestUrl(requestUrl);
    }

    @Override
    public IamTypeSystemVo queryByTypeCode(String companyCode, String applyTypeCode) {
        IamTypeSystemVo iamTypeSystemVo = new IamTypeSystemVo();
        iamTypeSystemVo.setApplyTypeCode(applyTypeCode);
        iamTypeSystemVo.setApplyTypeName(iamApplyTypeBaseMapper.getApplyTypeName(applyTypeCode));
        List<IamApplySystem> applySystemList = iamApplySystemMapper.queryByApplyTypeCode(companyCode, applyTypeCode);
        iamTypeSystemVo.setApplyList(applySystemList);
        return iamTypeSystemVo;
    }

    /**
     * 功能描述：根据应用编码查询应用相关信息
     * param：applyCodes
     * param：companyCode
     * @return List<IamApplySystem>
     */
    @Override
    public List<IamApplySystem> queryApplySystemByApplyCode(Set<String> applyCodes, String companyCode) {
        List<IamApplySystem> iamApplySystems = iamApplySystemMapper.queryApplySystemByApplyCode(applyCodes, companyCode);
        if(CollectionUtils.isEmpty(iamApplySystems)){
            throw new RuntimeException("根据应用编码查询不到任何应用相关信息！");
        }
        return iamApplySystems;
    }

    /**
     * 根据前端链接url获取应用类型
     * @param applyUrl
     * @return
     */
    @Override
    public String queryApplyTypeCode(String applyUrl) {
        String applyTypeCode = iamApplyTypeBaseMapper.queryApplyTypeCode(applyUrl);
        return applyTypeCode;
    }



}
